import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

import java.io.*;

public class Main {

    private static String rootPath = "";
    private static String oldProjectName = "psyduck";
    private static String newProjectName = "psyduck";
    private static String cnProjectName = "后台管理系统";

    public static File changeDirectory(String path) throws IOException {
        File rootDirectory = new File(path);
        File newRootDirectory = new File(rootDirectory.getCanonicalPath().replace(oldProjectName, newProjectName));
        rootDirectory.renameTo(newRootDirectory);
        return newRootDirectory;
    }

    public static void changeContent(String rootCanonicalPath, String regex, String replacement) throws IOException {
        String content = FileUtil.readUtf8String(rootCanonicalPath);
        FileUtil.writeUtf8String(content.replaceAll(regex, replacement), rootCanonicalPath);
    }

    public static void main(String[] args) {
        try {
            if(StrUtil.isNotBlank(System.getProperty("rootPath"))){
                rootPath = System.getProperty("rootPath");
            }
            if(StrUtil.isNotBlank(System.getProperty("oldProjectName"))){
                oldProjectName = System.getProperty("oldProjectName");
            }
            if(StrUtil.isNotBlank(System.getProperty("newProjectName"))){
                newProjectName = System.getProperty("newProjectName");
            }
            if(StrUtil.isNotBlank(System.getProperty("cnProjectName"))){
                cnProjectName = System.getProperty("cnProjectName");
            }
            if (StrUtil.isBlank(rootPath)) {
                rootPath = new File("").getCanonicalPath() + File.separator;
            }
            changeContent(rootPath + oldProjectName + File.separator + "pom.xml", oldProjectName + "-", newProjectName + "-");
            changeContent(rootPath + oldProjectName + File.separator + "pom.xml", "<name>" + oldProjectName, "<name>" + newProjectName);
            changeContent(rootPath + oldProjectName + File.separator + "pom.xml", "<artifactId>" + oldProjectName, "<artifactId>" + newProjectName);
            File[] rootFiles = changeDirectory(rootPath + oldProjectName).listFiles();
            for (File rootFile : rootFiles) {
                if(rootFile.isDirectory()){
                    String rootCanonicalPath = rootFile.getCanonicalPath();
                    if(rootCanonicalPath.indexOf(oldProjectName) != -1 && rootCanonicalPath.indexOf("-ui") == -1){
                        changeContent(rootCanonicalPath + File.separator + "pom.xml", oldProjectName + "-", newProjectName + "-");
                        changeContent(rootCanonicalPath + File.separator + "pom.xml", "<finalName>" + oldProjectName, "<finalName>" + newProjectName);
                        changeContent(rootCanonicalPath + File.separator + "pom.xml", "<artifactId>" + oldProjectName, "<artifactId>" + newProjectName);
                        changeDirectory(rootCanonicalPath);
                    }
                    if(rootCanonicalPath.indexOf("-ui") != -1){
                        changeContent(rootCanonicalPath + File.separator + "package.json", "\"name\": \".*\"", "\"name\": \""+newProjectName+"\"");
                        changeContent(rootCanonicalPath + File.separator + "vue.config.js", "outputDir: '.*'", "outputDir: './dist/"+newProjectName+"'");
                        changeContent(rootCanonicalPath + File.separator + "src\\settings.js", "title: '.*'", "title: '"+cnProjectName+"'");
                        changeContent(rootCanonicalPath + File.separator + "src\\views\\login\\index.vue", "<h3 class=\"title\">\\s*.*\\s*</h3>","<h3 class=\"title\">"+cnProjectName+"</h3>");
                        changeContent(rootCanonicalPath + File.separator + "src\\layout\\components\\Sidebar\\index.vue", "<span v-if=\"!isCollapse\">\\s*.*\\s*</span>","<span v-if=\"!isCollapse\">"+cnProjectName+"</span>");
                        changeContent(rootCanonicalPath + File.separator + "src\\layout\\components\\Sidebar\\Logo.vue", "title: '.*'", "title: '"+cnProjectName+"'");
                        changeDirectory(rootCanonicalPath);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
